﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Web;
using System.Xml;
using System.Xml.Serialization;

namespace TwilioSharp.Code.Twilio
{
    public static class TwilioExtensions
    {
        public static string ToXml(this ResponseType obj)
        {
            string content;
            var memStrm = new MemoryStream();

            using (var writer = new XmlTextWriter(memStrm, new UTF8Encoding(false)))
            {
                writer.Formatting = Formatting.Indented;

                var ns = new XmlSerializerNamespaces();
                ns.Add("", "");

                var serializer = new XmlSerializer(typeof(ResponseType));
                serializer.Serialize(writer, obj, ns);
                var utf8EncodedData = memStrm.GetBuffer();
                content = Encoding.UTF8.GetString(utf8EncodedData, 0, (int)memStrm.Length);
            }

            return content;
        }
    }
}